package com.midea.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class TestCyclicBarrier {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

        class Travel extends Thread{
            String name;
            String city;
            int time;

            public Travel(String name, String city, int time) {
                super();
                this.name = name;
                this.city = city;
                this.time = time;
            }
            @Override
            public void run(){
                System.out.println(name+"从"+city+"出发");
                try {
                    Thread.sleep(time);
                    System.out.println(name+"到达北京");
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }

                System.out.println(name+"去爬长城");
            }
        }

        Thread t1 = new Travel("张三","天津",2000);
        Thread t2 = new Travel("李四","黑龙江",5000);
        Thread t3 = new Travel("王二","拉萨",10000);

        t1.start();
        t2.start();
        t3.start();
    }
}
